package com.qding.park.manager.app.queue;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.manager.app.queue.handle.MonthPayedHandle;
import com.qding.park.manager.app.queue.handle.TempPayedHandle;
import com.qding.park.manager.app.queue.handle.TeneratPayedHandle;
import com.qding.park.manager.app.queue.util.SpringContextUtil;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.queue.QueueTypeEnum;

public class QueueOrderRunTask implements Runnable {

	private static final Log _log = LogFactory.getLog(QueueOrderRunTask.class);
	/**
	 * @Field @jedisClient : TODO(jedis Client)
	 */
	private JedisClient jedisClient;

	private IExceptionHandle exceptionHandle;

	public QueueOrderRunTask(JedisClient jedisClient, IExceptionHandle exceptionHandle) {
		this.jedisClient = jedisClient;
		this.exceptionHandle = exceptionHandle;
	}

	@Override
	public void run() {
		String key = CacheKeyThree.QUEUE_ORDER;
		while (true) {

			try {
				String value = jedisClient.rpop(key);
				
				if (StringUtils.isBlank(value)) {
					// 队列里面没有值
					Thread.sleep(1000 * 5);
				} else {
					QueueMsg msg = SerializeUtils.unSerialize(value, QueueMsg.class);
					
					HandleWrap.handle(msg);
					
//					if (msg.getEventType() == QueueTypeEnum.TEMP_PAYMENT_ORDER_PAYED_PARKHANDLE.getType()) {
//						//临停缴费 支付完成
//						TempPayedHandle tempPayedHandle = SpringContextUtil.getApplicationContext()
//								.getBean(TempPayedHandle.class);
//						tempPayedHandle.handle(msg);
//					}else if(msg.getEventType() == QueueTypeEnum.MONTHRENT_PAYMENT_ORDER_PAYED_PARKHANDLE.getType()){
//						//月租续费支付完成
//						MonthPayedHandle monthPayedHandle = SpringContextUtil.getApplicationContext()
//								.getBean(MonthPayedHandle.class);
//						monthPayedHandle.handle(msg);
//					}else if(msg.getEventType() == QueueTypeEnum.TENERAT_PAYMENT_ORDER_PAYED_PARKHANDLE.getType()){
//						//代缴支付完成
//						TeneratPayedHandle teneratPayedHandle = SpringContextUtil.getApplicationContext()
//								.getBean(TeneratPayedHandle.class);
//						teneratPayedHandle.handle(msg);
//					}else{
//						_log.info("非法的队列消息::" + value);
//					}
//					if (msg.getEventType() == QueueTypeEnum.BASE_DATA_SYNC.getType()) {
//						// 基础数据同步任务消息
//
//					} else if (msg.getEventType() == QueueTypeEnum.CARLOCK_TIMEOUT.getType()) {
//						// 锁车单超时消息
//						_log.info("锁车单超时消息:"+value);
//						CarLockTimeOutHandle carLockHandle = SpringContextUtil.getApplicationContext()
//								.getBean(CarLockTimeOutHandle.class);
//						carLockHandle.handle(msg);
//					} else if (msg.getEventType() == QueueTypeEnum.DATA_IDENTICAL.getType()) {
//						// 数据一致性检查消息
//					} else if (msg.getEventType() == QueueTypeEnum.FAILTASK_REDO.getType()) {
//						// 失败任务重做消息
//					} else if (msg.getEventType() == QueueTypeEnum.INVITE_TIMEOUT.getType()) {
//						// 邀请单超时消息
//						_log.info(" 邀请单超时消息 :"+value);
//						InviteTimeOutHandle inviteHandle = SpringContextUtil.getApplicationContext()
//								.getBean(InviteTimeOutHandle.class);
//						inviteHandle.handle(msg);
//					} else if (msg.getEventType() == QueueTypeEnum.PARK_CHARGE_RULE_SYNC.getType()) {
//						// 车场计费规则同步任务消息
//						_log.info(" 车场计费规则同步任务消息 :"+value);
//						ParkRuleSyncHandle parkRuleSyncHandle = SpringContextUtil.getApplicationContext().getBean(ParkRuleSyncHandle.class);
//						parkRuleSyncHandle.handle(msg);
//					} else if (msg.getEventType() == QueueTypeEnum.PARKINFO_SYNC.getType()) {
//						// 车场数据同步任务消息
//					} else if (msg.getEventType() == QueueTypeEnum.RESERVE_TIMEOUT.getType()) {
//						// 预定单超时处理消息
//						_log.info(" 预定单超时处理消息 :"+value);
//						ReserveTimeOutHandle reserveHandle = SpringContextUtil.getApplicationContext()
//								.getBean(ReserveTimeOutHandle.class);
//						reserveHandle.handle(msg);
//					} else if (msg.getEventType() == QueueTypeEnum.USERINFO_SYNC.getType()) {
//						// 用户基础数据同步任务消息
//						_log.info(" 用户基础数据同步任务消息 :"+value);
//						UserInfoSyncHandle userInfoSyncHandle = SpringContextUtil.getApplicationContext().getBean(UserInfoSyncHandle.class);
//						userInfoSyncHandle.handle(msg);
//					} else {
//						_log.info("非法的队列消息::" + value);
//					}
				}
			} catch (Exception e) {
				exceptionHandle.handle(e);
			}

		}

	}

}
